package bean.life.annotation;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * TODO  如果某个类实现了BeanFactoryPostProcessor接口，那么该类的生命周期会在
 *  registerBeanFactoryPostProcessor(beanFactory)时就执行，
 *  包括 创建实例，填充属性，执行setName，SetBeanFactory，init-method,afterProperties。
 *  因为需要先创建BeanFactoryPostProcessor对象，后期才能在实例化其他Bean的时候拦截
 */
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor, InitializingBean {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public MyBeanFactoryPostProcessor() {
		System.out.println("MyBeanFactoryPostProcessor 实例化");
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("【postProcessBeanFactory】");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("MyBeanFactoryPostProcessor ---> afterPropertiesSet");
	}
}